package com.xiaoran.actiiveNIO.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class JmsConsumer_topic {
    public static final String ACTIVEMQ_URL="tcp://192.168.171.128:61616";
    public static final String TOPIC_NAMQ="jdbc-topic";
    public static void main(String args[]) throws JMSException, IOException {
        System.out.println("***********z3");

        //1创建连接工场，，按照给定的url地址，采用默认的用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2通过连接工厂获得连接，获得connection并启动
        Connection connection = activeMQConnectionFactory.createConnection();

        //设置每个去订阅者（用户）的ID
        connection.setClientID("z3");

        //3.创建会话session
        Session session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4.创建目的地
        Topic topic=session.createTopic(TOPIC_NAMQ);

        //5,订阅
        TopicSubscriber topicSubscriber=session.createDurableSubscriber(topic,"xxxx..");

        //6.启动
        connection.start();

        //7.打印我们的消息
        Message message=topicSubscriber.receive();

        while (message!=null){
            TextMessage textMessage=(TextMessage)message;
            System.out.println("*****收到持久化的topic"+textMessage.getText());
            message=topicSubscriber.receive();
        }

        session.close();
        connection.close();
    }
}
